package com.attendance.www.hr.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.ApprovalRuleBean;
import com.attendance.www.hr.beans.ApprovalRulePageBean;
import com.attendance.www.hr.dao.mapper.ApprovalRuleExtMapper;
import com.attendance.www.hr.dao.mapper.ApprovalRuleMapper;
import com.attendance.www.hr.entitys.ApprovalRule;
import com.attendance.www.hr.entitys.ApprovalRuleExample;
import com.attendance.www.hr.entitys.ApprovalRuleExample.Criteria;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IApprovalRuleService;

@Service
public class ApprovalRuleServiceImpl implements IApprovalRuleService {
	
	@Resource
	private ApprovalRuleMapper approvalRuleDao;
	@Resource
	private ApprovalRuleExtMapper approvalRuleExtDao;
	
	/**
	 * 分页查询
	 * @param approvalRuleBean
	 * @param startNum
	 * @param pageCount
	 * @return
	 */
	@Override
	public ApprovalRulePageBean getApprovalRulePageList(ApprovalRuleBean approvalRuleBean, Integer startNum, Integer pageCount) {
		ApprovalRulePageBean approvalRulePageBean=new ApprovalRulePageBean();
		List<ApprovalRuleBean> approvalRuleBeanList=approvalRuleExtDao.getApprovalRuleList(approvalRuleBean, startNum, pageCount);
		Integer pageTotal=approvalRuleExtDao.getApprovalRuleCount(approvalRuleBean);
		approvalRulePageBean.setApprovalRuleBeanList(approvalRuleBeanList);
		approvalRulePageBean.setPageTotal(pageTotal);
		return approvalRulePageBean;
	};
	
	/**
	 * 保存验证
	 * @param approvalRuleBean
	 * @return
	 */
	@Override
	public boolean saveApprovalRuleValidate(ApprovalRuleBean approvalRuleBean){
		ApprovalRuleExample example=new ApprovalRuleExample();
		Criteria criteria=example.createCriteria();
		if(approvalRuleBean.getRequestType()!=null){
			criteria.andRequestTypeEqualTo(approvalRuleBean.getRequestType());
		}
		if(approvalRuleBean.getRequestRole()!=null){
			criteria.andRequestRoleEqualTo(approvalRuleBean.getRequestRole());
		}
		if(approvalRuleBean.getFromDays()!=null){
			criteria.andFromDaysEqualTo(approvalRuleBean.getFromDays());
		}
		if(approvalRuleBean.getToDays()!=null){
			criteria.andToDaysEqualTo(approvalRuleBean.getToDays());
		}
		if(approvalRuleBean.getLowestApprovalRole()!=null){
			criteria.andLowestApprovalRoleEqualTo(approvalRuleBean.getLowestApprovalRole());
		}
		if(approvalRuleBean.getOneApprovalRole()!=null){
			criteria.andOneApprovalRoleEqualTo(approvalRuleBean.getOneApprovalRole());
		}
		if(approvalRuleBean.getTwoApprovalRole()!=null){
			criteria.andTwoApprovalRoleEqualTo(approvalRuleBean.getTwoApprovalRole());
		}
		if(approvalRuleBean.getThreeApprovalRole()!=null){
			criteria.andThreeApprovalRoleEqualTo(approvalRuleBean.getThreeApprovalRole());
		}
		if(approvalRuleBean.getFourApprovalRole()!=null){
			criteria.andFourApprovalRoleEqualTo(approvalRuleBean.getFourApprovalRole());
		}
		if(approvalRuleBean.getFiveApprovalRole()!=null){
			criteria.andFiveApprovalRoleEqualTo(approvalRuleBean.getFiveApprovalRole());
		}
		List<ApprovalRule> approvalRuleList=approvalRuleDao.selectByExample(example);
		if(approvalRuleList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 保存验证
	 * @param approvalRuleBean
	 * @return
	 */
	@Override
	public boolean updateApprovalRuleValidate(ApprovalRuleBean approvalRuleBean){
		ApprovalRuleExample example=new ApprovalRuleExample();
		example.createCriteria().andApprovalRuleIdEqualTo(approvalRuleBean.getApprovalRuleId());
		List<ApprovalRule> approvalRuleList=approvalRuleDao.selectByExample(example);
		if(approvalRuleList.size()>0){
			if(approvalRuleList.get(0).getUpdateTime().compareTo(approvalRuleBean.getUpdateTime())!=0){
				throw new ResultFaultException("0_00011");
			}
		}
	    example=new ApprovalRuleExample();
	    Criteria criteria=example.createCriteria();
	    criteria.andApprovalRuleIdNotEqualTo(approvalRuleBean.getApprovalRuleId());
		if(approvalRuleBean.getRequestType()!=null){
			criteria.andRequestTypeEqualTo(approvalRuleBean.getRequestType());
		}
		if(approvalRuleBean.getRequestRole()!=null){
			criteria.andRequestRoleEqualTo(approvalRuleBean.getRequestRole());
		}
		if(approvalRuleBean.getFromDays()!=null){
			criteria.andFromDaysEqualTo(approvalRuleBean.getFromDays());
		}
		if(approvalRuleBean.getToDays()!=null){
			criteria.andToDaysEqualTo(approvalRuleBean.getToDays());
		}
		if(approvalRuleBean.getLowestApprovalRole()!=null){
			criteria.andLowestApprovalRoleEqualTo(approvalRuleBean.getLowestApprovalRole());
		}
		if(approvalRuleBean.getOneApprovalRole()!=null){
			criteria.andOneApprovalRoleEqualTo(approvalRuleBean.getOneApprovalRole());
		}
		if(approvalRuleBean.getTwoApprovalRole()!=null){
			criteria.andTwoApprovalRoleEqualTo(approvalRuleBean.getTwoApprovalRole());
		}
		if(approvalRuleBean.getThreeApprovalRole()!=null){
			criteria.andThreeApprovalRoleEqualTo(approvalRuleBean.getThreeApprovalRole());
		}
		if(approvalRuleBean.getFourApprovalRole()!=null){
			criteria.andFourApprovalRoleEqualTo(approvalRuleBean.getFourApprovalRole());
		}
		if(approvalRuleBean.getFiveApprovalRole()!=null){
			criteria.andFiveApprovalRoleEqualTo(approvalRuleBean.getFiveApprovalRole());
		}
		approvalRuleList=approvalRuleDao.selectByExample(example);
		if(approvalRuleList.size()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 添加
	 * @param ApprovalRuleBean
	 */
	@Override
	public boolean saveApprovalRule(ApprovalRuleBean approvalRuleBean){
		ApprovalRule approvalRule=new ApprovalRule();
		approvalRule.setRequestType(approvalRuleBean.getRequestType());
		approvalRule.setRequestRole(approvalRuleBean.getRequestRole());
		approvalRule.setFromDays(approvalRuleBean.getFromDays());
		approvalRule.setToDays(approvalRuleBean.getToDays());
		approvalRule.setLowestApprovalRole(approvalRuleBean.getLowestApprovalRole());
		approvalRule.setOneApprovalRole(approvalRuleBean.getOneApprovalRole());
		if(approvalRuleBean.getRequestRole().intValue()==99){
			approvalRule.setOneApprovalRole(99);
		}
		approvalRule.setTwoApprovalRole(approvalRuleBean.getTwoApprovalRole());
		approvalRule.setThreeApprovalRole(approvalRuleBean.getThreeApprovalRole());
		approvalRule.setFourApprovalRole(approvalRuleBean.getFourApprovalRole());
		approvalRule.setFiveApprovalRole(approvalRuleBean.getFiveApprovalRole());
		int record = approvalRuleDao.insertSelective(approvalRule);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 修改
	 * @param ApprovalRuleBean
	 */
	@Override
	public boolean updateApprovalRule(ApprovalRuleBean approvalRuleBean){
		ApprovalRule approvalRule=approvalRuleDao.selectByPrimaryKey(approvalRuleBean.getApprovalRuleId());
		approvalRule.setApprovalRuleId(approvalRuleBean.getApprovalRuleId());
		approvalRule.setRequestType(approvalRuleBean.getRequestType());
		approvalRule.setRequestRole(approvalRuleBean.getRequestRole());
		approvalRule.setFromDays(approvalRuleBean.getFromDays());
		approvalRule.setToDays(approvalRuleBean.getToDays());
		approvalRule.setLowestApprovalRole(approvalRuleBean.getLowestApprovalRole());
		approvalRule.setOneApprovalRole(approvalRuleBean.getOneApprovalRole());
		if(approvalRuleBean.getRequestRole().intValue()==99){
			approvalRule.setOneApprovalRole(99);
		}
		approvalRule.setTwoApprovalRole(approvalRuleBean.getTwoApprovalRole());
		approvalRule.setThreeApprovalRole(approvalRuleBean.getThreeApprovalRole());
		approvalRule.setFourApprovalRole(approvalRuleBean.getFourApprovalRole());
		approvalRule.setFiveApprovalRole(approvalRuleBean.getFiveApprovalRole());
		approvalRule.setUpdateTime(new Date());
		int record = approvalRuleDao.updateByPrimaryKey(approvalRule);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	/**
	 * 删除
	 * @return
	 */
	@Override
	public boolean deleteApprovalRule(Integer approvalRuleId){
		int record = approvalRuleDao.deleteByPrimaryKey(approvalRuleId);
		if (record > 0) {
			return true;
		} else {
			return false;
		}
	}

	
}
